/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils.sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import net.groupme.model.DashboardAnnouncement;
import net.groupme.model.Group;
import net.groupme.model.ObjectFactory;
import net.groupme.model.dao.DATABASE_CONSTANTS;

import org.springframework.jdbc.object.MappingSqlQuery;

/**
 * 
 * This class provides...
 * This interface defines...
 * 
 * @author  Mischa Frank, <a href="mailto:code@planetfrank.net">code@planetfrank.net</a>
 * 
 * @version 0.1a, created on 14.05.2007 
 *
 */

public class GetDashboardAnnouncementsQuery extends MappingSqlQuery {
	private DataSource datasource;
	public GetDashboardAnnouncementsQuery(DataSource ds, String query) {
        super(ds, query);
        this.datasource = ds;
        compile();
    }

    protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        List<DashboardAnnouncement> announcements = new ArrayList<DashboardAnnouncement>();
         while (!rs.isAfterLast()){
             DashboardAnnouncement announcement = new DashboardAnnouncement(
            		 rs.getInt(DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_USER_ID),
            		 rs.getInt(DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_ID),
            		 rs.getInt(DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_ID),
            		 rs.getInt(DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_TAG_ID),
            		 rs.getString(DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_ACTION),
            		 rs.getTimestamp(DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_TIME_AND_DATE),
            		 rs.getString(DATABASE_CONSTANTS.USER_GROUP_USERGROUP_ROLE),
            		 rs.getString("g" + DATABASE_CONSTANTS.RESOURCE_NAME),
            		 rs.getString(DATABASE_CONSTANTS.RESOURCE_NAME),
            		 rs.getString(DATABASE_CONSTANTS.TAG_KEYWORD)
            		 );
             announcements.add(announcement);
             logger.info("Announcement found: " + announcement.getAction());
        	 rs.next();
        }
    	return announcements; 
    }

}